﻿Imports Futuros.DAO
Imports Futuros.model

Namespace BL
    Public Class BLCostos
        Public Shared Function calcularMontos(ByVal lsDetList As List(Of xm_cierre_det), ByVal holSeg As Double, _
                                              ByVal tasaSeg As Double, ByVal plazoVenta As Double, ByVal overhead As Double, _
                                              ByVal merma As Double, ByVal fact As Double, ByVal interes As Double, _
                                              ByVal classid As String, ByVal origen As String, ByVal puerto As String, ByVal perpost As String) As List(Of xm_cierre_det)
            Dim htCostos As Hashtable = generarHashCostos(lsDetList)
            Dim htCostosFijos As Hashtable = generarHashCostos(obtenerCostosFijos(puerto, origen, classid, perpost))
            For Each c As xm_cierre_det In lsDetList
                ' Verificamos si el valor es un campo calculable
                If c.Tipo = "C" Or c.Tipo = "F" Then
                    'If c.Costid.Trim = "OVH" Then
                    '    MsgBox("Hello")
                    'End If
                    c.Total_cost = calcularCosto(c, lsDetList, htCostos, htCostosFijos, holSeg, tasaSeg, plazoVenta, overhead, merma, fact, interes)
                    htCostos.Remove(c.Costid.Trim)
                    htCostos.Add(c.Costid.Trim, c.Total_cost)
                End If
            Next
            Return lsDetList
        End Function

        Private Shared Function calcularCosto(ByVal c As xm_cierre_det, ByVal lsCostos As List(Of xm_cierre_det), ByVal htCostos As Hashtable, ByVal htCostosFijos As Hashtable, ByVal holSeg As Double, ByVal tasaSeg As Double, ByVal plazoVenta As Double, ByVal overhead As Double, ByVal merma As Double, ByVal fact As Double, ByVal interes As Double) As Double
            Select Case c.Costid.Trim
                Case "FOB"
                    Return sumarCostos(lsCostos, "FOB") * fact
                    'Return (htCostos.Item("FUT") + htCostos.Item("PRI")) * fact
                Case "CFR"
                    Return sumarCostos(lsCostos, "CFR")
                    'Return htCostos.Item("FOB") + htCostos.Item("FLEB")
                Case "SEG"
                    Return (htCostos.Item("CFR") * (1 + holSeg / 100)) * tasaSeg / 100
                Case "CIF"
                    Return sumarCostos(lsCostos, "CIF")
                    'Return htCostos.Item("SEG") + htCostos.Item("CFR")
                Case "TOT1"
                    Return sumarCostos(lsCostos, "TOT1")
                Case "MER"
                    Return (htCostos.Item("TOT1")) * merma / 100
                Case "OVH"
                    Return c.Total_cost
                Case "INT"
                    Return (htCostos.Item("TOT1") * interes / 100) / 360 * plazoVenta
                Case "VAP"
                    Return htCostos.Item("TOT1") + sumarCostos(lsCostos, c.Costid.Trim)
                Case Else
                    If c.Tipo = "F" Then
                        Return htCostosFijos.Item(c.Costid.Trim)
                    Else
                        Return sumarCostos(lsCostos, c.Costid.Trim)
                    End If
            End Select

        End Function
        Private Shared Function generarHashCostos(ByVal lsDetList As List(Of xm_cierre_det)) As Hashtable
            Dim htAux As Hashtable = New Hashtable
            For Each c As xm_cierre_det In lsDetList
                htAux.Add(c.Costid.Trim, c.Total_cost)
            Next
            Return htAux
        End Function
        Private Shared Function sumarCostos(ByVal lsDetList As List(Of xm_cierre_det), ByVal costid As String) As Double
            Dim aux As Double = 0.0
            For Each c As xm_cierre_det In lsDetList
                If c.Grupo.Trim = costid.Trim Then aux += c.Total_cost
            Next
            Return aux
        End Function
        Public Shared Function guardarCostos(ByVal custid As String, ByVal classid As String, ByVal fecha_op As String, _
                                             ByVal origen As String, ByVal pto_destino As String, ByVal costoContable As String, _
                                             ByVal motonave As String, ByVal qty_cierre As String, ByVal um_cierre As String, _
                                             ByVal precio_venta As String, ByVal id_cbot_futuro As String, _
                                             ByVal id_cbot_prima As String, ByVal perpost As String, ByVal holg_seguro As String, _
                                             ByVal tasa_seguro As String, ByVal dias_plazo_vta As String, ByVal overHead As String, _
                                             ByVal cant_contratos As String, ByVal monto_spred As String, _
                                             ByVal tasa_merma As String, ByVal tip_venta As String, ByVal tip_despacho As String, _
                                             ByVal futuro_compra As String, ByVal estado As String, ByVal tip_pres As String, _
                                             ByVal lsCostos As List(Of xm_cierre_det)) As String

            Return DAOCostos.guardarCostos(custid, classid, fecha_op, origen, pto_destino, costoContable, motonave, qty_cierre, _
                                           um_cierre, precio_venta, id_cbot_futuro, id_cbot_prima, perpost, holg_seguro, _
                                           tasa_seguro, dias_plazo_vta, overHead, cant_contratos, monto_spred, tasa_merma, _
                                           tip_venta, tip_despacho, futuro_compra, estado, tip_pres, lsCostos)

        End Function
        Public Shared Sub guardarCostos(ByVal cierreop As String, ByVal custid As String, ByVal classid As String, _
                                        ByVal fecha_op As String, ByVal origen As String, ByVal pto_destino As String, _
                                        ByVal costoContable As String, ByVal motonave As String, ByVal qty_cierre As String, _
                                        ByVal um_cierre As String, ByVal precio_venta As String, ByVal id_cbot_futuro As String, _
                                        ByVal id_cbot_prima As String, ByVal perpost As String, ByVal holg_seguro As String, _
                                        ByVal tasa_seguro As String, ByVal dias_plazo_vta As String, ByVal overHead As String, _
                                        ByVal cant_contratos As String, ByVal monto_spred As String, _
                                        ByVal tasa_merma As String, ByVal tip_venta As String, ByVal tip_despacho As String, _
                                        ByVal futuro_compra As String, ByVal estado As String, _
                                        ByVal tip_pres As String, ByVal lsCostos As List(Of xm_cierre_det))

            DAOCostos.guardarCostos(cierreop, custid, classid, fecha_op, origen, pto_destino, costoContable, motonave, qty_cierre, _
                                    um_cierre, precio_venta, id_cbot_futuro, id_cbot_prima, perpost, holg_seguro, _
                                    tasa_seguro, dias_plazo_vta, overHead, cant_contratos, monto_spred, tasa_merma, _
                                    tip_venta, tip_despacho, futuro_compra, estado, tip_pres, lsCostos)
        End Sub
        Public Shared Function cargarCostoEnc(ByVal cierreop As String) As xm_cierre_enc
            Return DAOCostos.cargarCostoEnc(cierreop)
        End Function
        Public Shared Function cargarCostoDet(ByVal cierreop As String) As List(Of xm_cost_item)
            Return DAOCostos.cargarCostoDet(cierreop)
        End Function
        Public Shared Function calcularCompensacion(ByVal futcompra As Double, ByVal futuro As Double, ByVal qty As Double) As Double
            Return (futcompra - futuro) * qty
        End Function
        Public Shared Function calcularContratos(ByVal qtyCierre As String, ByVal fact_contrato As Double) As Double
            If fact_contrato = 0 Then Return 0 Else Return qtyCierre \ fact_contrato
        End Function
        Public Shared Function calcularSpread(ByVal precVenta As Double, ByVal TALM As Double) As Double
            Return precVenta - TALM
        End Function
        Public Shared Function obtenerCbotProducto(ByVal Classid As String) As xm_cbot_prod
            Return DAOCostos.obtenerCBOTProducto(Classid)
        End Function
        Public Shared Function obtenerCbotMes(ByVal Classid As String) As List(Of xm_cbot_mes)
            Return DAOCostos.obtenercBOTMes(Classid)
        End Function
        Public Shared Function obtenerCbotMesByID(ByVal id_cbot_futuro As String) As Object
            Return DAOCostos.obtenercBOTMesById(id_cbot_futuro)
        End Function
        Public Shared Function obtenerCierres() As List(Of String)
            Return DAOCostos.obtenerCierres()
        End Function
        Public Shared Function obtenerCostos() As List(Of xm_cost_item)
            Return DAOCostos.obtenerCostos
        End Function
        Public Shared Function obtenerCostosFijos(ByVal puerto As String, ByVal origen As String, ByVal classid As String, ByVal perpost As String) As List(Of xm_cierre_det)
            Return DAOCostos.obtenerCostosFijos(puerto, origen, classid, perpost)
        End Function
        Public Shared Function obtenerClientes() As List(Of String)
            Return DAOCostos.obtenerClientes
        End Function
        Public Shared Function obtenerParametros() As xm_config_per
            Return DAOCostos.obtenerParametros()
        End Function
        Public Shared Function obtenerPerpost() As List(Of String)
            Return DAOCostos.obtenerPerpost
        End Function
        Public Shared Function obtenerMotonaves() As List(Of segdef)
            Return DAOCostos.obtenerMotonaves
        End Function
        Public Shared Function obtenerMotonaveById(ByVal id As String) As segdef
            Return DAOCostos.obtenerMotonavePorId(id)
        End Function
        Public Shared Function obtenerProductos() As List(Of String)
            Return DAOCostos.obtenerProductos()
        End Function
        Public Shared Function obtenerOrigenes() As List(Of String)
            Return DAOCostos.obtenerOrigen()
        End Function
        Public Shared Function obtenerPuertos() As List(Of String)
            Return DAOCostos.obtenerPuertos()
        End Function
        Public Shared Function obtenerPresentaciones() As List(Of segdef)
            Return DAOCostos.obtenerPresentaciones()
        End Function
        Public Shared Function obtenerTipoDespacho() As List(Of tipodespacho)
            Return DAOCostos.obtenerTipoDespacho
        End Function
        Public Shared Function obtenerTipoProductos() As List(Of String)
            Return DAOCostos.obtenerTipoProductos()
        End Function
        Public Shared Function obtenerUms() As List(Of String)
            Return DAOCostos.obtenerUMs()
        End Function
        Public Shared Function obtenerUmByClassId(ByVal classid As String) As String
            Return DAOCostos.obtenerUmByClassId(classid)
        End Function
        Public Shared Function validarCliente(ByVal customer As String) As String
            Return DAOCostos.obtenerCodCliente(customer)
        End Function
        Public Shared Function validarOrigen(ByVal origen As String)
            Return DAOCostos.obtenerCodOrigen(origen)
        End Function
        Public Shared Function validarPuerto(ByVal puerto As String)
            Return DAOCostos.obtenerCodPuerto(puerto)
        End Function
        Public Shared Function obtenerNombreCliente(ByVal custid As String) As String
            Return DAOCostos.obtenerNombreCliente(custid)
        End Function
        Public Shared Function obtenerNombreOrigen(ByVal codpais As String) As String
            Return DAOCostos.obtenerNombreOrigen(codpais)
        End Function
        Public Shared Function obtenerNombrePuerto(ByVal codpto As String) As String
            Return DAOCostos.obtenerNombrePuerto(codpto)
        End Function



    End Class
End Namespace

